package com.yyplatform.admin.platform.role.repository;

import java.util.Date;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.yyplatform.admin.platform.role.model.entity.SysUserEntity;


/**
 * Created on 2024-09-12
 */
public interface SysUserRepository extends JpaRepository<SysUserEntity, Long>, JpaSpecificationExecutor<SysUserEntity> {
    SysUserEntity findByUsername(String userName);


    @Query(
            value =
                    "SELECT * FROM sys_user WHERE 1=1 "
                            + " AND if(:enabled is not null,  `enabled` = :enabled, 1=1 ) "
                            + " AND if(:username != '' , `username` LIKE concat('%', :username, '%'), 1=1 )  "
                            + " AND if(:phone != '',  `phone` LIKE concat('%', :phone, '%'), 1=1 ) "
                            + " AND if(:realName != '' , `real_name` = :realName, 1=1 ) "
                            + " AND (create_time >= :beginTime AND create_time <= :endTime) ",
            countQuery =
                    "SELECT count(id) FROM sys_user WHERE 1=1 "
                            + " AND if(:enabled is not null,  `enabled` = :enabled, 1=1 ) "
                            + " AND if(:username != '' , `username` LIKE concat('%', :username, '%'), 1=1 )  "
                            + " AND if(:phone != '',  `phone` LIKE concat('%', :phone, '%'), 1=1 ) "
                            + " AND if(:realName != '' , `real_name` = :realName, 1=1 ) "
                            + " AND (create_time >= :beginTime AND create_time <= :endTime )",
            nativeQuery = true)
    Page<SysUserEntity> findPage(
            @Param("enabled") Boolean enabled,
            @Param("username") String username,
            @Param("phone") String phone,
            @Param("realName") String realName,
            @Param("beginTime") Date beginTime,
            @Param("endTime") Date endTime,
            @Param("pageable") Pageable pageable);


    @Query(
            value =
                    "SELECT * FROM sys_user WHERE 1=1 "
                            + " AND id in (select  xx.user_id from  sys_user_dept xx where xx.dept_id = :deptId )"
                            + " AND if(:enabled is not null,  `enabled` = :enabled, 1=1 ) "
                            + " AND if(:username != '' , `username` LIKE concat('%', :username, '%'), 1=1 )  "
                            + " AND if(:phone != '',  `phone` LIKE concat('%', :phone, '%'), 1=1 ) "
                            + " AND if(:realName != '' , `real_name` = :realName, 1=1 ) "
                            + " AND (create_time >= :beginTime AND create_time <= :endTime) ",
            countQuery =
                    "SELECT count(id) FROM sys_user WHERE 1=1 "
                            + " AND id in (select  xx.user_id from  sys_user_dept xx where xx.dept_id = :deptId )"
                            + " AND if(:enabled is not null,  `enabled` = :enabled, 1=1 ) "
                            + " AND if(:username != '' , `username` LIKE concat('%', :username, '%'), 1=1 )  "
                            + " AND if(:phone != '',  `phone` LIKE concat('%', :phone, '%'), 1=1 ) "
                            + " AND if(:realName != '' , `real_name` = :realName, 1=1 ) "
                            + " AND (create_time >= :beginTime AND create_time <= :endTime )",
            nativeQuery = true)
    Page<SysUserEntity> findPageWithDept(
            @Param("deptId") Long deptId,
            @Param("enabled") Boolean enabled,
            @Param("username") String username,
            @Param("phone") String phone,
            @Param("realName") String realName,
            @Param("beginTime") Date beginTime,
            @Param("endTime") Date endTime,
            @Param("pageable") Pageable pageable);


}
